﻿using System;
using System.IO.IsolatedStorage;

namespace GTasksPlus.Code
{
    public class GTasksSettings
    {
        #region Public Properties

        public const string OAuthUrl = "https://accounts.google.com/o/oauth2/auth";
        public const string OAuthTokenUrl = "https://accounts.google.com/o/oauth2/token";
        public const string Scope = "https://www.googleapis.com/auth/tasks";
        public const string ClientId = "INSERT YOUR GOOGLE API CLIENT ID HERE";
        public const string RedirectUri = "http://localhost";
        public const string ClientSecret = "INSERT YOUR GOOGLE API CLIENT SECRET ID HERE";
        public const string ApiKey = "INSERT YOUR GOOGLE API KEY HERE";
        public const int RequestTimeout = 10*1000;
        public const string ApplicationTitle = "GTasks Plus";

        public static int TokenExpiry
        {
            get
            {
                var settings = IsolatedStorageSettings.ApplicationSettings;
                return (int) (!settings.Contains("TokenExpiry") ? 0 : settings["TokenExpiry"]);
            }
            set
            {
                var settings = IsolatedStorageSettings.ApplicationSettings;
                settings["TokenExpiry"] = value;
            }
        }

        public static string TokenCode
        {
            get
            {
                var settings = IsolatedStorageSettings.ApplicationSettings;
                return !settings.Contains("TokenCode") ? string.Empty : settings["TokenCode"].ToString();
            }
            set
            {
                var settings = IsolatedStorageSettings.ApplicationSettings;
                settings["TokenCode"] = value;
            }
        }

        public static string AccessToken
        {
            get
            {
                var settings = IsolatedStorageSettings.ApplicationSettings;
                return !settings.Contains("AccessToken") ? string.Empty : settings["AccessToken"].ToString();
            }
            set
            {
                var settings = IsolatedStorageSettings.ApplicationSettings;
                settings["AccessToken"] = value;
                settings["Timestamp"] = TimeSpan.FromTicks(DateTime.Now.Ticks).TotalSeconds;
            }
        }

        public static string RefreshToken
        {
            get
            {
                var settings = IsolatedStorageSettings.ApplicationSettings;
                return !settings.Contains("RefreshToken") ? string.Empty : settings["RefreshToken"].ToString();
            }
            set
            {
                var settings = IsolatedStorageSettings.ApplicationSettings;
                settings["RefreshToken"] = value;
            }
        }

        #endregion

        #region Public Methods

        public static void ClearSession()
        {
            TokenCode = RefreshToken = AccessToken = String.Empty;
            IsolatedStorageSettings.ApplicationSettings.Clear();
        }

        public static bool IsLoggedIn()
        {
            var settings = IsolatedStorageSettings.ApplicationSettings;
            return settings.Contains("Timestamp") && settings.Contains("AccessToken") && !string.IsNullOrEmpty(TokenCode) && !string.IsNullOrEmpty(AccessToken) && !string.IsNullOrEmpty(RefreshToken);
        }

        #endregion
    }
}
